package br.usp.ime.ingpos.testes.services;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.ResourceBundle;

import org.junit.Before;
import org.junit.Test;
import org.mockito.Mockito;

import br.com.caelum.vraptor.core.Localization;
import br.com.caelum.vraptor.util.test.MockLocalization;
import br.usp.ime.ingpos.modelo.AreaDePesquisa;
import br.usp.ime.ingpos.modelo.Candidato;
import br.usp.ime.ingpos.modelo.Email;
import br.usp.ime.ingpos.modelo.Inscricao;
import br.usp.ime.ingpos.modelo.ProcessoSeletivo;
import br.usp.ime.ingpos.modelo.TipoProcessoSeletivo;
import br.usp.ime.ingpos.modelo.Usuario;
import br.usp.ime.ingpos.modelo.dao.AreaDePesquisaDAO;
import br.usp.ime.ingpos.modelo.dao.CandidatoDAO;
import br.usp.ime.ingpos.modelo.dao.InscricaoDAO;
import br.usp.ime.ingpos.modelo.dao.ProcessoSeletivoDao;
import br.usp.ime.ingpos.modelo.dao.UsuarioDao;
import br.usp.ime.ingpos.services.AreaDePesquisaService;
import br.usp.ime.ingpos.services.CandidatoService;
import br.usp.ime.ingpos.services.EmailException;
import br.usp.ime.ingpos.services.EmailService;
import br.usp.ime.ingpos.services.InscricaoService;
import br.usp.ime.ingpos.services.OrientadorService;
import br.usp.ime.ingpos.services.ProcessoSeletivoService;
import br.usp.ime.ingpos.services.UsuarioService;
import br.usp.ime.ingpos.testes.BancoDeDadosTestCase;
import br.usp.ime.ingpos.web.controllers.UsuarioSessao;

public class CursoServiceTeste
    extends
        BancoDeDadosTestCase
{
    private Usuario usuario;
    private UsuarioSessao usuarioSessao;
    private UsuarioService usuarioService;
    private CandidatoService candidatoService;
    private InscricaoService inscricaoService;
    private AreaDePesquisaService areaDePesquisaService;
    private ProcessoSeletivoService processoSeletivoService;
    private OrientadorService orientadorService;
    private EmailService emailService;

    public CursoServiceTeste(
        String name )
    {
        super( name );
    }

    @Before
    protected void setUp()
        throws Exception
    {
        super.setUp();

        final UsuarioDao usuarioDao = new UsuarioDao( getSessionCreator() );
        this.usuario = usuarioDao.procurarPorEmail( RegistroNovoUsuarioServiceTeste.EMAIL );
        this.usuarioSessao = new UsuarioSessao();
        this.usuarioSessao.setUsuario( usuario );
        this.usuarioService = new UsuarioService( usuarioDao, usuarioSessao );
        this.areaDePesquisaService = new AreaDePesquisaService( new AreaDePesquisaDAO(
            getSessionCreator() ) );
        this.processoSeletivoService = new ProcessoSeletivoService( new ProcessoSeletivoDao(
            getSessionCreator() ), emailService, new MockLocalization() );

        emailService = Mockito.mock(EmailService.class);
        
        this.candidatoService = new CandidatoService(
            usuarioSessao,
            usuarioService,
            new CandidatoDAO( getSessionCreator() ),
            emailService,
            new Localization() {
                
                @Override
                public String getMessage(
                    String arg0,
                    Object... arg1 )
                {
                    // TODO Auto-generated method stub
                    return null;
                }
                
                @Override
                public Locale getLocale()
                {
                    // TODO Auto-generated method stub
                    return null;
                }
                
                @Override
                public Locale getFallbackLocale()
                {
                    // TODO Auto-generated method stub
                    return null;
                }
                
                @Override
                public ResourceBundle getBundle()
                {
                    // TODO Auto-generated method stub
                    return null;
                }
            });

        this.inscricaoService = new InscricaoService(
            usuarioSessao,
            usuarioService,
            new InscricaoDAO( getSessionCreator() ),
            candidatoService,
            processoSeletivoService,
            orientadorService);
    }

    @Test
    public void testeInserirOuAtualizarInscricao()
    {
        Inscricao inscricao = inscricaoService.getInscricaoAtual();
        if( inscricao == null ) {
            inscricao = new Inscricao();
        }

        inscricao.setDedicacaoExclusiva( "sim" );
        inscricao.setExposicaoPropositos( "propositos" );
        inscricao.setOutrosProcessosSeletivos( "outros" );
        inscricao.setObservacoes( "observacoes" );
        inscricao.setSabeAreasDeInteresse( false );
        ProcessoSeletivo processoSeletivo = processoSeletivoService.buscarProcessoSeletivoVigentePorTipo( TipoProcessoSeletivo.MESTRADO );
        assertNotNull(processoSeletivo);
        inscricao.setProcessoSeletivo( processoSeletivo );

        List<AreaDePesquisa> areas = new ArrayList<AreaDePesquisa>();
        areas.add( areaDePesquisaService.procuraAreaDePesquisaPorNome( "Banco de Dados" ) );
        areas.add( areaDePesquisaService.procuraAreaDePesquisaPorNome( "Criptografia" ) );

        inscricao.setAreasDeInteresseAfinidade1( areas );

        Candidato candidato = (Candidato) usuario;

        assertNotNull( candidato );

        inscricaoService.inserirOuAtualizarInscricao( inscricao );

        Inscricao inscricaoAtualizada = inscricaoService.getInscricaoAtual();
        assertNotNull( inscricaoAtualizada.getInscricaoID() );
    }
}
